package com.iyundao.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.ActivityService;
import com.iyundao.io.model.ActivityModel;
import com.iyundao.io.model.ActivityModelLookBack;
import com.iyundao.io.model.PartyMember;
import com.iyundao.io.model.SysUserProvider;
import com.iyundao.io.tools.*;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @ApiOperation(value="添加活动", notes="添加活动")
    @RequestMapping(value = "/addactivity", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData addActivity(@RequestBody JSONObject params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        ActivityAdd activityAdd = JSON.toJavaObject(params, ActivityAdd.class);
        ActivityModel1 activityModel1 = activityAdd.getActivityModel();
        Integer i = activityAdd.getArray().length;
        ActivityModel activityModel = new ActivityModel();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            //activityModel.setActivitycreatetime(format.parse(activityModel1.getActivitycreatetime()));
            activityModel.setActivitynumber(i);
            activityModel.setActivitycontent(activityModel1.getActivitycontent());
            activityModel.setActivitydigest(activityModel1.getActivitydigest());
            activityModel.setActivityendtime(format.parse(activityModel1.getActivityendtime()));
            activityModel.setActivityid(activityModel1.getActivityid());
            activityModel.setActivitylatitude(activityModel1.getActivitylatitude());
            activityModel.setActivitylongitude(activityModel1.getActivitylongitude());
            activityModel.setActivitysignendtime(format.parse(activityModel1.getActivitysignendtime()));
            activityModel.setActivitysitename(activityModel1.getActivitysitename());
            activityModel.setUserid(sysUser.getId());
            activityModel.setOrganizationid(sysUser.getSysDept().getSid());
            activityModel.setActivitytitle(activityModel1.getActivitytitle());
            activityModel.setActivitystarttime(format.parse(activityModel1.getActivitystarttime()));
            activityModel.setActivitysignstarttime(format.parse(activityModel1.getActivitysignstarttime()));
            activityModel.setActivityimg(activityModel1.getActivityimg());
            activityModel.setActivityjoinnumber(0);
            activityModel.setActivityintegral(activityModel1.getActivityintegral());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ResponseData responseData = activityService.insert(activityModel);
        if (responseData.getCode() == 200) {
            Integer lastId = activityService.selectLastId();
            return activityService.joinPeople(activityAdd.getArray(), lastId);
        } else {
            return responseData;
        }
    }

    @ApiOperation(value="根据id查询活动", notes="根据id查询活动")
    @RequestMapping(value = "/selectbyid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectById(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.selectById(id);
    }

    @ApiOperation(value="根据组织id查询活动", notes="根据组织id查询活动")
    @RequestMapping(value = "/selectbyorganizationid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAllByOrganizationId(@RequestBody String params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String organizationid = sysUser.getSysDept().getSid();
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");

        return activityService.selectAllByOrganizationId(pagenum, pagesize, organizationid);
    }

    @ApiOperation(value="党员用户查询本组织下已通过审核的活动", notes="党员用户查询本组织下已通过审核的活动")
    @RequestMapping(value = "/selectbypersonal", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectByPersonal(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        String activityTitle = (String) object.get("activityTitle");
        return activityService.selectAllByLike(pagenum,pagesize,activityTitle);
    }

    @ApiOperation(value="根据组织id和状态查询活动", notes="根据组织id和状态查询活动")
    @RequestMapping(value = "/selectbyflag", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAllByFlag(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        Integer activityflag = (Integer) object.get("activityFlag");
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        String organizationid = sysUser.getSysDept().getSid();
        return activityService.findByOrganizationIdAndFlag(pagenum, pagesize, organizationid, activityflag);
    }

    @ApiOperation(value="取消发布", notes="取消发布")
    @RequestMapping(value = "/updateflag", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData updateFlag(@RequestBody JSONObject params){
        ActivityModel activityModel = JSON.toJavaObject(params, ActivityModel.class);
        Integer id = activityModel.getId();
        Integer activityflag = activityModel.getActivityflag();
        return activityService.updateFlag(activityflag, id);
    }

    @ApiOperation(value="查询活动允许的人员", notes="查询活动允许的人员")
    @RequestMapping(value = "/selectalljoin", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public List<PartymemberVo> selectAllJoinByActivityId(@RequestBody String params) {
        Integer activityid = (Integer)JSONObject.parseObject(params).get("id");
        return activityService.selectAllJoinByActivityId(activityid);
    }

    @ApiOperation(value="搜索活动", notes="搜索活动")
    @RequestMapping(value = "/selectactivitybylike", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectByLike(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        String title = (String) object.get("activityTitle");
        return activityService.selectByLike(pagenum, pagesize,title);
    }

    @ApiOperation(value="搜索本组织下活动", notes="搜索本组织下活动")
    @RequestMapping(value = "/selectbylikeandSid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectbylikeandSid(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        String title = (String) object.get("activityTitle");
        return activityService.selectByLikeAndSid(pagenum, pagesize,title);
    }

    @ApiOperation(value="搜索本组织下精彩回顾", notes="搜索本组织下精彩回顾")
    @RequestMapping(value = "/selectlookbackbylikeandsid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectLookBackByLikeAndSid(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        String title = (String) object.get("activityTitle");
        return activityService.selectLookBackByLikeAndSid(pagenum, pagesize,title);
    }

    @ApiOperation(value="根据活动id获取活动相信信息以及与活动相关的所有图片", notes="根据活动id获取活动相信信息以及与活动相关的所有图片")
    @RequestMapping(value = "/selectallimage", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectAllImage(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.selectAllImageById(id);
    }

    @ApiOperation(value="根据用户id查询活动", notes="根据用户id查询活动")
    @RequestMapping(value = "/selectbyuserid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectByRoleId(@RequestBody String params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        String sid = sysUser.getSysDept().getSid();
        return activityService.selectByUserId(pagenum,pagesize,sid);
    }

    @ApiOperation(value="根据活动id查询活动相关内容", notes="根据活动id查询活动相关内容")
    @RequestMapping(value = "/selectshowbyid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectShowById(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.selectShowById(id);
    }

    @ApiOperation(value="查询通过审核的活动", notes="查询通过审核的活动")
    @RequestMapping(value = "/selectall", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAll(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        return activityService.selectAll(pagenum,pagesize);
    }

    @ApiOperation(value="通过管理员id查询通过审核的活动", notes="通过管理员id查询通过审核的活动")
    @RequestMapping(value = "/selectallby", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAllBy(@RequestBody String params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        String organizationid = sysUser.getSysDept().getSid();
        return activityService.selectAllBy(pagenum,pagesize,organizationid);
    }

    @ApiOperation(value="查询所有精彩回顾", notes="查询所有精彩回顾")
    @RequestMapping(value = "/selectalllookback", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAllLookBack(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        return activityService.selectAllLookBack(pagenum,pagesize);
    }

    @ApiOperation(value="根据组织id查询所有精彩回顾", notes="查询所有精彩回顾")
    @RequestMapping(value = "/selectalllookbackbysid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAllLookBackBySid(@RequestBody String params){
        JSONObject object = JSONObject.parseObject(params);
        Integer pagenum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        return activityService.selectAllLookBackBySid(pagenum,pagesize);
    }

    @ApiOperation(value="发布精彩回顾", notes="发布精彩回顾")
    @RequestMapping(value = "/insertlookback", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData insertLookBack(@RequestBody JSONObject params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        ActivityModel1 activityModel1 = JSON.toJavaObject(params, ActivityModel1.class);
        ActivityModelLookBack activityModel = new ActivityModelLookBack();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            activityModel.setActivitycontent(activityModel1.getActivitycontent());
            activityModel.setActivitydigest(activityModel1.getActivitydigest());
            activityModel.setActivityendtime(format.parse(activityModel1.getActivityendtime()));
            activityModel.setActivityid(activityModel1.getActivityid());
            activityModel.setActivitysitename(activityModel1.getActivitysitename());
            activityModel.setUserid(sysUser.getId());
            activityModel.setOrganizationid(sysUser.getSysDept().getSid());
            activityModel.setActivitytitle(activityModel1.getActivitytitle());
            activityModel.setActivitystarttime(format.parse(activityModel1.getActivitystarttime()));
            activityModel.setActivityimg(activityModel1.getActivityimg());
            activityModel.setActivitycreatetime(new Date());
            activityModel.setActivityflag(0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return activityService.insertLookBack(activityModel);
    }

    @ApiOperation(value="修改精彩回顾状态", notes="修改精彩回顾状态")
    @RequestMapping(value = "/updatelookback", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData updateFlag(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.updateLookBackFlag(id);

    }

    @ApiOperation(value="审核通过", notes="审核通过")
    @RequestMapping(value = "/updateactivity", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData updateActivity(@RequestBody JSONObject params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        ActivityAdd activityAdd = JSON.toJavaObject(params, ActivityAdd.class);
        ActivityModel1 activityModel1 = activityAdd.getActivityModel();
        Integer i = activityAdd.getArray().length;
        ActivityModel activityModel = new ActivityModel();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            activityModel.setId(activityModel1.getId());
            activityModel.setActivitynumber(i);
            activityModel.setActivitycontent(activityModel1.getActivitycontent());
            activityModel.setActivitydigest(activityModel1.getActivitydigest());
            activityModel.setActivityendtime(format.parse(activityModel1.getActivityendtime()));
            activityModel.setActivityid(activityModel1.getActivityid());
            activityModel.setActivitylatitude(activityModel1.getActivitylatitude());
            activityModel.setActivitylongitude(activityModel1.getActivitylongitude());
            activityModel.setActivitysignendtime(format.parse(activityModel1.getActivitysignendtime()));
            activityModel.setActivitysitename(activityModel1.getActivitysitename());
            activityModel.setUserid(sysUser.getId());
            activityModel.setOrganizationid(activityModel1.getOrganizationid());
            activityModel.setActivitytitle(activityModel1.getActivitytitle());
            activityModel.setActivitystarttime(format.parse(activityModel1.getActivitystarttime()));
            activityModel.setActivitysignstarttime(format.parse(activityModel1.getActivitysignstarttime()));
            activityModel.setActivityimg(activityModel1.getActivityimg());
            activityModel.setActivityjoinnumber(0);
            activityModel.setActivityintegral(activityModel1.getActivityintegral());
            activityModel.setActivitycreatetime(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        ResponseData responseData = activityService.updateActivity(activityModel);
        if (responseData.getCode() == 200) {
            Integer id = activityModel1.getId();
            return activityService.updatePeople(activityAdd.getArray(), id);
        } else {
            return responseData;
        }
    }

    @ApiOperation(value="修改精彩回顾", notes="修改精彩回顾")
    @RequestMapping(value = "/updateactivitylookback", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData updateLookBack(@RequestBody JSONObject params){
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        ActivityModel1 activityModel1 = JSON.toJavaObject(params, ActivityModel1.class);
        ActivityModelLookBack activityModel = new ActivityModelLookBack();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            activityModel.setId(activityModel1.getId());
            activityModel.setActivitycontent(activityModel1.getActivitycontent());
            activityModel.setActivitydigest(activityModel1.getActivitydigest());
            activityModel.setActivityendtime(format.parse(activityModel1.getActivityendtime()));
            activityModel.setActivityid(activityModel1.getActivityid());
            activityModel.setActivitysitename(activityModel1.getActivitysitename());
            activityModel.setUserid(sysUser.getId());
            activityModel.setOrganizationid(activityModel1.getOrganizationid());
            activityModel.setActivitytitle(activityModel1.getActivitytitle());
            activityModel.setActivitystarttime(format.parse(activityModel1.getActivitystarttime()));
            activityModel.setActivityimg(activityModel1.getActivityimg());
            activityModel.setActivityflag(0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return activityService.updateActivityLookBack(activityModel);
    }

    @ApiOperation(value="根据id查询精彩回顾", notes="根据id查询精彩回顾")
    @RequestMapping(value = "/selectlookbackbyid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectLookBackById(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.selectLookBackById(id);
    }

    @ApiOperation(value="不分组织，查询所有的精彩回顾", notes="不分组织，查询所有的精彩回顾")
    @RequestMapping(value = "/selectalllookbackbylike", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<ActivityModelVo> selectAllLookBackByLike(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        String search = (String) object.get("search");
        return activityService.selectAllLooKBackByLike(pageNum,pageSize,search);
    }

    @ApiOperation(value="活动删除", notes="活动删除")
    @RequestMapping(value = "/deleteactivity", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData delete(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.deleteActivity(id);
    }

    @ApiOperation(value="精彩回顾删除", notes="精彩回顾删除")
    @RequestMapping(value = "/deletelookback", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData deleteLookBack(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return activityService.deleteActivityLookBack(id);
    }
}
